package com.czkt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.BasDict;
import com.czkt.pojo.CstService;
import com.czkt.pojo.SysUser;
import com.czkt.pojo.vo.SysUserVo;
import com.czkt.service.IBasDictService;
import com.czkt.service.ICstCustomerService;
import com.czkt.service.ICstServiceService;
import com.czkt.service.ISysUserService;
import com.czkt.utils.PageUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.security.auth.Subject;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/service")
public class CstServiceController {

    @Autowired
    IBasDictService basDictService;

    @Autowired
    ICstServiceService cstServiceService;

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    ICstCustomerService cstCustomerService;

    @RequestMapping("/list")
    public Object list(CstService cstService, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "2") Integer pageSize, Model model) {
        IPage<CstService> page = new Page<>(pageIndex, pageSize);

        SysUserVo sysUserVo = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(CstService::getSvrCreateId, sysUserVo.getUsrId())
                .like(StringUtils.hasText(cstService.getSvrCustName()), CstService::getSvrCustName, cstService.getSvrCustName())
                .like(StringUtils.hasText(cstService.getSvrTitle()), CstService::getSvrTitle, cstService.getSvrTitle())
                .eq(StringUtils.hasText(cstService.getSvrType()), CstService::getSvrType, cstService.getSvrType());

        page = cstServiceService.page(page, queryWrapper);

        PageUtils pageUtils = new PageUtils();

        BeanUtils.copyProperties(page, pageUtils);

        model.addAttribute("servicePage", pageUtils);

        QueryWrapper<BasDict> basDictQueryWrapper = new QueryWrapper<>();
        basDictQueryWrapper.lambda().eq(BasDict::getDictType, "服务类型");

        model.addAttribute("dicts", basDictService.list(basDictQueryWrapper));

        model.addAttribute("svrCustName", cstService.getSvrCustName());
        model.addAttribute("svrTitle", cstService.getSvrTitle());

        return "service/list";
    }

    @RequestMapping("/save")
    public Object save(CstService cstService) {
        SysUserVo sysUserVo = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        cstService.setSvrCreateId(sysUserVo.getUsrId());
        cstService.setSvrCreateBy(sysUserVo.getUsrName());

        cstService.setSvrStatus("新创建");

        if (cstServiceService.saveOrUpdate(cstService)) {
            return "redirect:list";
        } else {
            return "redirect:service/add";
        }

    }

    @RequestMapping("/add")
    public Object add(Model model) {

        SysUserVo sysUserVo = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        model.addAttribute("customers", cstCustomerService.list());

        QueryWrapper<BasDict> basDictQueryWrapper = new QueryWrapper<>();
        basDictQueryWrapper.lambda().eq(BasDict::getDictType, "服务类型");

        model.addAttribute("dicts", basDictService.list(basDictQueryWrapper));

        return "service/add";
    }

    @RequestMapping("/dispatch")
    public Object dispatch(CstService cstService, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "2") Integer pageSize, Model model) {
        IPage<CstService> page = new Page<>(pageIndex, pageSize);
        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.hasText(cstService.getSvrCustName()), CstService::getSvrCustName, cstService.getSvrCustName())
                .like(StringUtils.hasText(cstService.getSvrTitle()), CstService::getSvrTitle, cstService.getSvrTitle())
                .eq(StringUtils.hasText(cstService.getSvrType()), CstService::getSvrType, cstService.getSvrType())
                .eq(CstService::getSvrStatus, "新创建");

        page = cstServiceService.page(page, queryWrapper);

        PageUtils pageUtils = new PageUtils();

        BeanUtils.copyProperties(page, pageUtils);

        model.addAttribute("servicePage", pageUtils);

        QueryWrapper<BasDict> basDictQueryWrapper = new QueryWrapper<>();
        basDictQueryWrapper.lambda().eq(BasDict::getDictType, "服务类型");

        model.addAttribute("dicts", basDictService.list(basDictQueryWrapper));

        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.lambda().eq(SysUser::getUsrRoleId, 4);

        model.addAttribute("users", sysUserService.list(sysUserQueryWrapper));
        model.addAttribute("svrCustName", cstService.getSvrCustName());
        model.addAttribute("svrTitle", cstService.getSvrTitle());
        return "service/dispatch";
    }

    @RequestMapping("/allot/{svrId}/{svrDueId}")
    @ResponseBody
    public Object allot(@PathVariable long svrId, @PathVariable long svrDueId, String userName) {
        CstService cstService = new CstService();

        SysUserVo sysUserVo = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        cstService.setSvrDueId(sysUserVo.getUsrId());
        cstService.setSvrDueTo(sysUserVo.getUsrName());

        cstService.setSvrId(svrId);

        cstService.setSvrStatus("已分配");

        cstService.setSvrDealId(svrDueId);
        cstService.setSvrDealBy(userName);

        return cstServiceService.updateById(cstService);
    }

    @RequestMapping("/deal")
    public Object deal(CstService cstService, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "2") Integer pageSize, Model model) {
        IPage<CstService> page = new Page<>(pageIndex, pageSize);
        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();

        SysUserVo sysUserVo = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        queryWrapper.lambda()
                .like(StringUtils.hasText(cstService.getSvrCustName()), CstService::getSvrCustName, cstService.getSvrCustName())
                .like(StringUtils.hasText(cstService.getSvrTitle()), CstService::getSvrTitle, cstService.getSvrTitle())
                .eq(StringUtils.hasText(cstService.getSvrType()), CstService::getSvrType, cstService.getSvrType())
                .eq(CstService::getSvrStatus, "已分配")
                .eq(CstService::getSvrDealId, sysUserVo.getUsrId());

        page = cstServiceService.page(page, queryWrapper);

        PageUtils pageUtils = new PageUtils();

        BeanUtils.copyProperties(page, pageUtils);

        model.addAttribute("page", pageUtils);

        QueryWrapper<BasDict> basDictQueryWrapper = new QueryWrapper<>();
        basDictQueryWrapper.lambda().eq(BasDict::getDictType, "服务类型");

        model.addAttribute("dicts", basDictService.list(basDictQueryWrapper));

        model.addAttribute("svrCustName", cstService.getSvrCustName());
        model.addAttribute("svrTitle", cstService.getSvrTitle());

        return "service/deal";
    }

    @RequestMapping("/edit")
    public Object edit(long svrId, Model model) {

        model.addAttribute("serviced", cstServiceService.getById(svrId));

        return "service/dealEdit";
    }

    @RequestMapping("/editDeal")
    public Object editDeal(CstService cstService) {
        cstService.setSvrStatus("已处理");
        if (cstServiceService.updateById(cstService)) {
            return "redirect:deal";
        } else {
            return "redirect:service/edit?svrId=" + cstService.getSvrId();
        }
    }

    @RequestMapping("/feedbackEdit")
    public Object feedbackEdit(long svrId, Model model) {
        model.addAttribute("serviced", cstServiceService.getById(svrId));
        return "service/feedbackEdit";
    }

    @RequestMapping("/editFB")
    public Object editFB(CstService cstService) {
        if (cstService.getSvrSatisfy() < 3) {
            cstService.setSvrStatus("新创建");
        } else {
            cstService.setSvrStatus("已归档");
        }
        if (cstServiceService.updateById(cstService)) {
            return "redirect:list";
        } else {
            return "redirect:service/feedbackEdit?svrId=" + cstService.getSvrId();
        }
    }

    @RequestMapping("/del/{svrId}")
    @ResponseBody
    public Object del(@PathVariable() Long svrId, Model model) {
        Map resultMap = new HashMap();
        resultMap.put("delResult", cstServiceService.removeById(svrId));
        return resultMap;
    }

    @RequestMapping("/arch")
    public Object arch(CstService cstService, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "2") Integer pageSize, Model model) {

        IPage<CstService> page = new Page<>(pageIndex, pageSize);

        SysUserVo sysUserVo = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.hasText(cstService.getSvrCustName()), CstService::getSvrCustName, cstService.getSvrCustName())
                .like(StringUtils.hasText(cstService.getSvrTitle()), CstService::getSvrTitle, cstService.getSvrTitle())
                .eq(StringUtils.hasText(cstService.getSvrType()), CstService::getSvrType, cstService.getSvrType())
                .eq(CstService::getSvrStatus, "已归档")
                .eq(CstService::getSvrCreateId, sysUserVo.getUsrId());

        page = cstServiceService.page(page, queryWrapper);

        PageUtils pageUtils = new PageUtils();

        BeanUtils.copyProperties(page, pageUtils);

        model.addAttribute("servicePage",pageUtils);

        QueryWrapper<BasDict> basDictQueryWrapper = new QueryWrapper<>();
        basDictQueryWrapper.lambda().eq(BasDict::getDictType, "服务类型");

        model.addAttribute("dicts", basDictService.list(basDictQueryWrapper));

        model.addAttribute("svrCustName", cstService.getSvrCustName());
        model.addAttribute("svrTitle", cstService.getSvrTitle());

        return "service/arch";
    }

    @RequestMapping("/findArch")
    public Object findArch(long svrId,Model model){

        model.addAttribute("serviced",cstServiceService.getById(svrId));

        return "service/archInfo";
    }
}
